package com.yyfax.ph.progress.supplement;


import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class SupplementPresenter extends Presenter<SupplementModel, SupplementPresenter.View> {

	protected static final String CANCEL_FAILURE = "终止失败!";
	protected static final String CANCEL_SUCCESS = "终止成功!";
	protected static final String CANCEL_RECOMMEND_WARNING_CODE = "10006";
	protected static final String SUPPLEMENT_SUCCESS = "补件成功!";
	protected static final String SUPPLEMENT_FAILURE = "补件失败!";
	protected static final String SUPPLEMENT_WARNING_CODE = "10007";
	public static final int REQUEST_CODE_CANCEL = 156;
	public static final int REQUEST_CODE_SUPPLEMENT = 166;

	public static class Generator implements PresenterFactory<SupplementPresenter> {
		@Override
		public SupplementPresenter create() {
			return new SupplementPresenter();
		}
	}

	public interface View extends IView {
		void onCancelSuccess(String tips);

		void onCancelFailure(String tips);

		void onCancelFailureExit(String tips);

		void onUploadSuccess(String tips);

		void onUploadFailure(String tips);

		void onUploadFailureExit(String tips);
	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new SupplementModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void cancelRecommend(String id) {
		SupplementModel.Cancel cancel = new SupplementModel.Cancel();
		cancel.rcmdId = id;
		execute(cancel, REQUEST_CODE_CANCEL, true);
	}


	public void uploadCredit(String rcmdId, List<String> imgIds) {
		SupplementModel.Supplement supplement = new SupplementModel.Supplement(rcmdId, imgIds);
		execute(supplement, REQUEST_CODE_SUPPLEMENT, true);

	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void cancelResult(SupplementModel.CancelResult result) {
		if (isResponseSuccess(result)) {
			mView.onCancelSuccess(CANCEL_SUCCESS);
		} else if (isResponseWarning(result) && null != result.getContent() &&
				!TextUtils.isEmpty(result.getContent().code) && CANCEL_RECOMMEND_WARNING_CODE.equals(result.getContent().code)) {
			//取消进件失败且code不为空：10006 取消进件失败
			mView.onCancelFailureExit(result.getMsg());
		} else {
			mView.onCancelFailure(TextUtils.isEmpty(result.getMsg()) ? CANCEL_FAILURE : result.getMsg());
		}
	}


	@Subscribe(threadMode = ThreadMode.MAIN)
	public void uploadResult(SupplementModel.SupplementResult result) {
		StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_D008, result.getStatus());
		if (isResponseSuccess(result)) {
			mView.onUploadSuccess(SUPPLEMENT_SUCCESS);
		} else if (isResponseWarning(result) && null != result.getContent() &&
				!TextUtils.isEmpty(result.getContent().code) && SUPPLEMENT_WARNING_CODE.equals(result.getContent().code)) {
			//补件失败且code不为空：10007 补件失败
			mView.onUploadFailureExit(result.getMsg());
		} else {
			mView.onUploadFailure(TextUtils.isEmpty(result.getMsg()) ? SUPPLEMENT_FAILURE : result.getMsg());
		}
	}
}
